package com.example.bluetoothlib.test;

import java.util.ArrayList;

import com.example.bluetoothlib.R;
import com.example.bluetoothlib.model.BluetoothHelper;
import com.example.bluetoothlib.model.IBTEventHandler;

import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener,IBTEventHandler{

	static final int REQUEST_CODE_ENABLE_BLUETOOTH=1;
	static final int REQUEST_CODE_ENABLE_BT_DISCOVERABLE=2;
	EditText editText;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		BluetoothHelper.getInstance().setEventHandler(this);
		
		findViewById(R.id.button1).setOnClickListener(this);
		findViewById(R.id.button2).setOnClickListener(this);
		findViewById(R.id.button3).setOnClickListener(this);
		findViewById(R.id.button4).setOnClickListener(this);
		findViewById(R.id.button5).setOnClickListener(this);
		findViewById(R.id.button6).setOnClickListener(this);
		editText=(EditText)findViewById(R.id.editText1);
		final TextView textView=(TextView)findViewById(R.id.textView1);
		BluetoothHelper.getInstance().registerBroadcastReceiver(this);
		
		Thread thread=new Thread(new Runnable() {
			
			@Override
			public void run()
			{
				while(true)
				{
					final String tmp=BluetoothHelper.getInstance().readMessage();
					textView.post(new Runnable() {
						
						@Override
						public void run() {
							textView.setText(tmp);
						}
					});
				}
			}
		});
		
		thread.start();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) 
	{
		switch (v.getId()) {
		case R.id.button1:
			BluetoothHelper.getInstance().promptToEnableBT(this, REQUEST_CODE_ENABLE_BLUETOOTH);
			break;
		case R.id.button2:
			BluetoothHelper.getInstance().promptToEnableDiscoverability(this, REQUEST_CODE_ENABLE_BT_DISCOVERABLE);
			break;
		case R.id.button3:
			BluetoothHelper.getInstance().searchDevices();
			break;
		case R.id.button4:
			BluetoothHelper.getInstance().listenForConnections();
			break;
		case R.id.button5:
			BluetoothHelper.getInstance().connectToServer(0);
			break;
		case R.id.button6:
			BluetoothHelper.getInstance().sendMessage(editText.getText().toString());
			break;
		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case REQUEST_CODE_ENABLE_BLUETOOTH:
			if(resultCode==RESULT_OK)
			{
				Toast.makeText(this, "BT Enabled", Toast.LENGTH_SHORT).show();
			}
			break;
		case REQUEST_CODE_ENABLE_BT_DISCOVERABLE:
			if (resultCode==RESULT_OK) 
			{
				Toast.makeText(this, "discover Enabled", Toast.LENGTH_SHORT).show();
			}
			break;
		default:
			break;
		}
	}

	@Override
	protected void onDestroy() {
		BluetoothHelper.getInstance().unregisterBroadcastReceiver(this);
		super.onDestroy();
	}

	@Override
	public void onDiscoverStart() {
		setTitle("Start Discover!");
	}

	@Override
	public void onDiscoverFinished(ArrayList<BluetoothDevice> devices) {
		setTitle("Find devices:"+devices.size());
	}

	@Override
	public void onConnectionEstablished() {
		setTitle("Connection Established");
	}
	
	
}
